/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: server.c
> Author:cjy
> Mail: 
> Created Time: 2024年08月15日 星期四 11时21分58秒
***************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

typedef struct user_info{
	int id;
	char name[10];
}USE;
int main(int argc,const char* argv[])
{
	
	int sock = 0;
	int listenfd = 0;
	int ret = 0;


	struct sockaddr_in addr;
    	memset(&addr,0,sizeof(addr));

	sock = socket(AF_INET,SOCK_STREAM,0);
	if(sock == -1){
		perror("sock");
		exit(-1);
	}

	addr.sin_family = AF_INET;
        addr.sin_port = htons(6666);
        addr.sin_addr.s_addr = inet_addr("192.168.198.128");

	ret = bind(sock,(struct sockaddr *)&addr,sizeof(struct sockaddr));
	if(ret == -1)
	{
		perror("bind");
		exit(-1);
	}


	listen(sock,5);

	listenfd = accept(sock,NULL,NULL);
	if(listenfd == -1)
	{
		perror("accpet");
		exit(-1);
	}

	USE buf;
	USE dest;
 while(1)
 {
	ret = recv(listenfd,&buf,sizeof(buf),0);
	if(ret == -1)
	{
		perror("recv");
		exit(-1);
	}
	
	printf("id is %d\n",buf.id);
	printf("name is %s\n",buf.name);


	printf("please input id:\n");
	scanf("%d",&dest.id);
	printf("please input name:\n");
	scanf("%s",dest.name);
	ret = send(listenfd,&dest,sizeof(dest),0);
	if(ret == -1)
	{
		perror("send");
		exit(-1);
	}
 }


	close(listenfd);
	close(sock);


    return 0;
}
